package com.etbsms.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.etbsms.dao.IUsersDAO;
import com.etbsms.domain.Role;
import com.etbsms.domain.Users;
import com.etbsms.form.QueryUsersForm;
import com.etbsms.security.User;

@Repository
@Component("usersDAO")
public class UsersDAOImpl extends GenericDAOImpl<Users, Integer> implements IUsersDAO{
	
	@Autowired
	public SessionFactory sessionFactory;	
	
	public User findUserByUsername(String username) {
		User user = null;
		Users users = this.get(username);			
		if (users != null && users.isEnabled())
			user = new User(users.getUsername(), users.getPassword(), users);
		return user;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public Users get(String username) {		
		Session session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(Users.class);
		criteria.add(Restrictions.eq("username", username));
		criteria.add(Restrictions.eq("enabled", true));
		List<Users> list = criteria.list();
		Users users = (list.size() == 1) ? list.get(0) : null;		

		for(Role _r : users.getRole())
			System.out.println(_r.getName());	
		
		session.close();
		return users;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Users> getAll() {		
		Session session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(Users.class);
		criteria.add(Restrictions.eq("enabled", true));
		List<Users> list = criteria.list();
		session.close();
		return list;
	}

	@Override
	public Users get(int seq_no) {
		Session session = sessionFactory.openSession();
		Users users = (Users)session.get(Users.class, seq_no);
	
		session.close();
		return users;
	}

	@Override
	public boolean save(Users entity) {
		entity.setCreated(new Date());
		entity.setEnabled(true);
		
		boolean result = false;
		Session session = sessionFactory.openSession();		
		try{
			Transaction tx = session.beginTransaction();
			session.save(entity);			
			tx.commit();		
			result = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		session.close();
		return result;
	}

	@Override
	public boolean update(Users entity) {
		boolean result = false;
		Session session = sessionFactory.openSession();		
		try{
			Transaction tx = session.beginTransaction();
			session.update(entity);			
			tx.commit();		
			result = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		session.close();
		return result;
	}

	@Override
	@SuppressWarnings("unchecked")
	public QueryUsersForm query(QueryUsersForm form) {
		String qr = "%" + form.getQr_str() + "%";		
		Session session = sessionFactory.openSession();		
		Criteria criteria = session.createCriteria(Users.class);		
		Disjunction disj = Restrictions.disjunction();
		disj.add(Restrictions.ilike("username", qr));
		disj.add(Restrictions.ilike("email", qr));
		disj.add(Restrictions.ilike("fullname", qr));
		criteria.add(disj);
		criteria.add(Restrictions.eq("enabled", true));		
		if(form.isRequery())
			form.setTotalRows(criteria.list().size());		
		criteria.setFirstResult(form.getFirstResult());
		criteria.setMaxResults(form.getRowsPerPage());		
		List<Users> list = criteria.list();		
		for(Users _u : list)
			for(Role _r : _u.getRole())
				System.out.println(_r.getName());
		form.setObj(list);
		session.close();
		return form;
	}

	@Override
	public boolean existUsername(String username) {
		Session session = sessionFactory.openSession();		
		Criteria criteria = session.createCriteria(Users.class);	
		criteria.add(Restrictions.eq("username", username));
		criteria.add(Restrictions.eq("enabled", true));
		boolean result = criteria.list().size() > 0;
		session.close();
		return result;
	}	
	
	@Override
	public boolean existEmail(String email) {
		Session session = sessionFactory.openSession();		
		Criteria criteria = session.createCriteria(Users.class);	
		criteria.add(Restrictions.eq("email", email));
		criteria.add(Restrictions.eq("enabled", true));
		boolean result = criteria.list().size() > 0;
		session.close();
		return result;
	}

	@Override
	@SuppressWarnings("unchecked")
	public Users get(String username, String password) {
		Session session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(Users.class);
		criteria.add(Restrictions.eq("username", username));
		criteria.add(Restrictions.eq("password", password));
		criteria.add(Restrictions.eq("enabled", true));
		List<Users> list = criteria.list();
		Users users = (list.size() == 1) ? list.get(0) : null;		
		if(users != null){
			for(Role _r : users.getRole())
				System.out.println(_r.getName());	
		}
		
		session.close();
		return users;
	}	
}
